
import HeadPrefab from "../../component/HeadPrefab";
import { EventManager } from "../../engine/EventManager";
import { Logger } from "../../engine/Logger";
import { SpriteFrameComponent } from "../../engine/SpriteFrameComponent";
import UIManager from "../../engine/UIManager";
import ViewBase from "../../engine/ViewBase";
import { HttpHelper } from "../../net/HttpHelper";
import { ServerConfig } from "../../net/ServerConfig";
import { SocketClient } from "../../net/SocketClient";
import { NativeSdkHelper } from "../../sdk/NativeSdkHelper";
import { EventConfig } from "../config/EventConfig";
import { GameConfig } from "../config/GameConfig";
import { ViewConfig } from "../config/ViewConfig";
import { AccountModel } from "../model/AccountModel";
import { UserModel } from "../model/UserModel";
import { UIHelper } from "../util/UIHelper";
import { ViewCheck } from "./ViewCheck";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewUserInfo extends ViewBase {

    private sexNode: cc.Node
    private txtName: cc.Label
    private nameNode: cc.Node

    private txtLocation: cc.Label

    private txtIp: cc.Label;
    private txtInvite: cc.Label;
    private btnBind: cc.Node
    private btnHuanVip: cc.Node;
    private btnCancelVip: cc.Node
    private btnBeVip: cc.Node
    private inviteData: object

    onEnable(): void {
        super.onEnable()
        EventManager.instance.on(EventConfig.Event_Update_UserInfo, this.refresh, this)
        this.nameNode = this.findChild(this.node, "nameNode")
        this.sexNode = this.findChild(this.nameNode, "sexNode")
        this.sexNode.getComponent(cc.Sprite).spriteFrame = this.sexNode.getComponent(SpriteFrameComponent).spriteFrameList[UserModel.instance.sex - 1]
        this.txtName = this.getChild(this.nameNode, "txtName", cc.Label)
        UIHelper.onAddEvent(this.nameNode, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewUserInfoFixName)
        }, cc.Button.Transition.NONE)
        let locationNode: cc.Node = this.findChild(this.node, "locationNode")
        this.txtLocation = this.getChild(locationNode, "txtLocation", cc.Label)
        this.txtIp = this.getChild(locationNode, "txtIp", cc.Label)
        this.txtIp.string = UserModel.instance.ip;
        this.txtInvite = this.getChild(this.node, "txtInvite", cc.Label)
        this.btnBind = this.findChild(this.node, "btnBind")
        this.btnHuanVip = this.findChild(this.node, "btnHuanVip")

        UIHelper.onAddEvent(this.btnHuanVip, () => {
            UIHelper.showCheck("换绑VIP码,需要充值50元RMB后解绑。是否现在充值", (isOk: boolean) => {
                if (isOk) {
                    HttpHelper.req_pay(6)
                }
            }, ViewCheck.Dialog_Type.All)
        })
        UIHelper.onAddEvent(this.btnBind, () => {
            // this.closeView()
            UIManager.instance.showView(ViewConfig.viewConfig.ViewInvite)
        })

        let btnweb: cc.Node = this.findChild(this.node, "btnweb")
        UIHelper.onAddEvent(btnweb, () => {
            let url: string = ServerConfig.getWebServerUrl() + "/uploadweb/?userid=" + UserModel.instance.userId + "&sign=" + AccountModel.instance.sign + "&account=" + encodeURIComponent(AccountModel.instance.account)
            cc.sys.openURL(url);
        })
        // let btnImg0: cc.Node = this.findChild(this.node, "btnImg0")
        // let btnImg1: cc.Node = this.findChild(this.node, "btnImg1")
        // UIHelper.onAddEvent(btnImg0, () => {
        //     NativeSdkHelper.selectPhoto(GameConfig.upload_pic_type.Img1)
        // })
        // UIHelper.onAddEvent(btnImg1, () => {
        //     NativeSdkHelper.selectPhoto(GameConfig.upload_pic_type.Img2)
        // })
        this.btnCancelVip = this.findChild(this.node, "btnCancelVip")
        UIHelper.onAddEvent(this.btnCancelVip, () => {
            UIHelper.showCheck("取消后,将失去推广员权限,名下玩家与您的绑定关系解除。是否确认取消?", (isOk: boolean) => {
                if (isOk) {
                    HttpHelper.req_pay(6)
                }
            }, ViewCheck.Dialog_Type.All)
        })
        this.btnBeVip = this.findChild(this.node, "btnBeVip")
        UIHelper.onAddEvent(this.btnBeVip, () => {
            UIHelper.showCheck("充值60元RMB,即可成为VIP,是否现在充值?", (isOk: boolean) => {
                if (isOk) {
                    HttpHelper.req_pay(5)
                }
            }, ViewCheck.Dialog_Type.All)
        })
        let txtId: cc.Label = this.getChild(this.node, "txtId", cc.Label);
        txtId.string = UserModel.instance.userId + ""

        // HttpHelper.get_user_data(UserModel.instance.invite, (data: object) => {
        //     this.inviteData = data
        //     this.refresh()
        // })
        this.refresh()
    }


    private refresh() {
        let head: HeadPrefab = this.getChild(this.node, "head", HeadPrefab)
        head.setHead(UserModel.instance.userPhoto, UserModel.instance.lv)
        UIHelper.onAddEvent(head.node, () => {
            // NativeSdkHelper.selectPhoto(GameConfig.upload_pic_type.HeadImg)
        })
        if (UserModel.instance.lv > 0) {
            this.btnBeVip.active = false
            this.btnCancelVip.active = true
        } else {
            this.btnBeVip.active = true
            this.btnCancelVip.active = false
        }
        if (UserModel.instance.invite && UserModel.instance.invite > 0) {
            this.btnBind.active = false
            this.btnHuanVip.active = true
        }
        else {
            this.btnBind.active = true
            this.btnHuanVip.active = false
        }
        if (UserModel.instance.invite) {
            // if(this.inviteData){
                // this.txtInvite.string = UserModel.instance.invite + ' '+this.inviteData["nick_name"]
                this.txtInvite.string = UserModel.instance.invite+""
            // }
        } else {
            this.txtInvite.string = ""
        }
        this.txtName.string = UserModel.instance.userName;
        if (UserModel.instance.locationName && UserModel.instance.locationName != "") {
            this.txtLocation.string = UserModel.instance.locationName
        } else {
            this.txtLocation.string = "未获取到位置"
        }
    }


}
